package com.android.bookgarden.jpushmsg;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;


import com.android.bookgarden.utli.LogUtil;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import static android.content.ContentValues.TAG;

/**
 * author: yulu
 * mailbox: yulu@cecpay.com
 * created at: 17/3/21 11:18
 */

public class JPushManager {

    public static void stopJPush() {
        LogUtil.i("JPushManager::stopJPush ", "stopJPush");
//        if (JPushInterface.getConnectionState(ContextUtil.getContext())) {
//        if (!JPushInterface.isPushStopped(ContextUtil.getContext())) {
//            LogUtil.i("JPushManager::stopJPush ", "JPush is stop");
//            JPushInterface.stopPush(ContextUtil.getContext());
//        }
    }

//    public static void startJPush() {
//        if (!JPushInterface.getConnectionState(ContextUtil.getContext())) {
//            int noticeState = AppSpUtils.getInstance().getInt(ContextUtil.getContext(), AppConfig.MSGSTARES, MsgIntent.OPENNOTIFIERPRO);
//            if (MsgIntent.OPENNOTIFIERPRO == noticeState) {
//                LogUtil.i("JPushManager::startJPush ", "startJPush");
//                JPushInterface.resumePush(ContextUtil.getContext());
//            }
//
//        }
//    }

    public static void initJPush(Context context) {
        JPushInterface.setDebugMode(true);
        JPushInterface.init(context);
        LogUtil.i("JPushManager::getRegistrationID ", JPushInterface.getRegistrationID(context));

    }

    private static final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.i(TAG, logs);
//                    AppSpUtils.getInstance().save(ContextUtil.getContext(), AppConfig.JPUSH_ALIAS, true);
                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.i(TAG, logs);
                    // 当设置不成功时，延迟 60 秒来调用 Handler 设置别名
//                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.e(TAG, logs);
            }
        }
    };


    /**
     * 只设置了别名alias，没有设置tag
     */
//    private static final Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(android.os.Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case MSG_SET_ALIAS:
//                    Log.d(TAG, "Set alias in handler.");
//                    // 调用 JPush 接口来设置别名。
//                    setAlias((String) msg.obj, null);
////                   在此处设置tag
////                    Set<String> pushTag = Utils.getJpushTag(ContextUtil.getContext());
////                    setAlias((String) msg.obj, pushTag);
//                    break;
//                default:
//                    Log.i(TAG, "Unhandled msg - " + msg.what);
//            }
//        }
//    };

    /**
     * 设置别名alias和标签tag
     *
     * @param alias
     * @param tag
     */
//    public static void setAlias(String alias, Set<String> tag) {
//        if (!AppSpUtils.getInstance().getBoolean(ContextUtil.getContext(), AppConfig.JPUSH_ALIAS, false)) {
//            JPushInterface.setAliasAndTags(ContextUtil.getContext(),
//                    alias,
//                    tag,
//                    mAliasCallback);
//        }
//    }

//    public static void getRegistrationID(Context context){
//        String registrationID = JPushInterface.getRegistrationID(context);
//        if (!TextUtils.isEmpty(registrationID)){
//            Comms.printLogDebug(" ---",""+registrationID);
//            AppUIHelper.bingPush(ContextUtil.getUserId(),registrationID);
//
//        }else{
//            JPushManager.setAlias(ContextUtil.getUserId(), null);
//        }
//    }


}
